﻿if ($.Event == null)
	$.Event = {};

$.Event.FileOpen = function()
{
	$.manager.fileopen(8, "$.Event.FileOpen_Callback");
}

$.Event.FileOpen_Callback = function( fileurl )
{
	$("input.imageUrl").val( fileurl );
}

$.Event.FileOpen2 = function()
{
	$.manager.fileopen(9, "$.Event.FileOpen2_Callback");
}

$.Event.FileOpen2_Callback = function( fileurl )
{
	$("input.bigImageUrl").val( fileurl );
}

$.Event.getList = function()
{
	$.ajaxNet(
		"/manager/event/eventmethod.aspx?_vb=GetList",
		{
			_CurrentOnly : false,
			PageNo : $("div.pager").pager("option", "currentPageNo"),
			PageSize : $("div.pager").pager("option", "pageSize")
		}, function(results, context)
		{
			$("table.eventList tbody").html(results[0]);
			$("div.pager").pager("update", results[1]);
		});
};

$.Event.showInputForm = function(eventEntity) {
	if (typeof(eventEntity) == "undefined")
	{
		$("div.inputForm")
			.find("input.eventSN").val("0").end()
			.find("input.eventStartString").val("").end()
			.find("input.eventEndString").val("").end()
			.find("input.imageUrl").val("").end()
			.find("input.bigImageUrl").val("").end()
			.find("input.title").val("").end()
			.find("textarea.content").val("").end()
			.find("input.show").removeAttr("checked").end()
			.find("input.show[value='1']").attr("checked", "checked").end()
			.show();
	}
	else
	{
		$("div.inputForm")
			.find("input.eventSN").val(eventEntity.EventSN).end()
			.find("input.eventStartString").val(eventEntity.StartDateString).end()
			.find("input.eventEndString").val(eventEntity.EndDateString).end()
			.find("input.imageUrl").val(eventEntity.ImageUrl).end()
			.find("input.bigImageUrl").val(eventEntity.BigImageUrl).end()
			.find("input.title").val(eventEntity.Title).end()
			.find("textarea.content").val(eventEntity.Content).end()
			.find("input.show").removeAttr("checked").end()
			.find("input.show[value='" + (eventEntity.Show?"1":"0") + "']").attr("checked", "checked").end()
//			.find("input.show").val(eventEntity.Show?"1":"0").end()
			.show();
	}
}

$.Event.cancelInputForm = function() {
	$("div.inputForm").hide();
}

$.Event.submitInputForm = function() {
	var eventSN = $("div.inputForm input.eventSN").val();
	var eventStartString = $("div.inputForm input.eventStartString").val();
	var eventEndString = $("div.inputForm input.eventEndString").val();
	var imageUrl = $("div.inputForm input.imageUrl").val();
	var bigImageUrl = $("div.inputForm input.bigImageUrl").val();
	var title = $("div.inputForm input.title").val();
	var content = $("div.inputForm textarea.content").val();
	var show = ($("div.inputForm input.show:checked").val()=="1");

	if (eventSN > 0)
	{
		$("div.inputForm").ajaxNet(
			"/manager/event/eventmethod.aspx?_vb=Modify",
			{
				"EventSN" : eventSN,
				"StartDateString" : eventStartString,
				"EndDateString" : eventEndString,
				"ImageUrl" : imageUrl,
				"BigImageUrl" : bigImageUrl,
				"Title" : title,
				"Content" : content,
				"Show" : show
			}, function(results, context)
			{
				$("div.inputForm").hide();
				alert('수정되었습니다.');
				$.Event.getList();
			});
	}
	else
	{
		$("div.inputForm").ajaxNet(
			"/manager/event/eventmethod.aspx?_vb=Create",
			{
				"StartDateString" : eventStartString,
				"EndDateString" : eventEndString,
				"ImageUrl" : imageUrl,
				"BigImageUrl" : bigImageUrl,
				"Title" : title,
				"Content" : content,
				"Show" : show
			}, function(results, context)
			{
				$("div.inputForm").hide();
				alert('추가되었습니다.');
				$("div.pager").pager("pageNo", 1);
				$.Event.getList();
			});
	}
}

$.Event.modify = function(elem) {
//	alert($(elem).closest("tr").length());
	var eventSN = $(elem).closest("tr").tagdata().EventSN;
	$.ajaxNet(
		"/manager/event/eventmethod.aspx?_vb=GetInfo",
		{
			"EventSN" : eventSN
		}, function(results, context)
		{
			$.Event.showInputForm(JSON.parse(results[0]));
		});
};

$.Event.remove = function(elem) {
	var eventSN = $(elem).closest("tr").tagdata().EventSN;

	$("table.managerList").ajaxNet(
		"/manager/event/eventmethod.aspx?_vb=Remove",
		{
			"EventSN" : eventSN
		}, function(results, context)
		{
			alert('삭제되었습니다.');
			$.Event.getList();
			
		});
}